unit FMain;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, Buttons, ExtCtrls, Printers, Grids, DBGrids, XDBGrids,
  IniFiles, ShellApi;

type
  TFormMain = class(TForm)
    Label1: TLabel;
    OpenDialog: TOpenDialog;
    gbDatos: TGroupBox;
    Panel1: TPanel;
    Label2: TLabel;
    eReporte: TEdit;
    BtnAbrir: TSpeedButton;
    BtnDisenar: TSpeedButton;
    BtnVer: TSpeedButton;
    Grid: TDBGrid;
    btnConexion: TSpeedButton;
    lConexion: TLabel;
    PrinterSetupDialog: TPrinterSetupDialog;
    Label3: TLabel;
    cbImpresora: TComboBox;
    btnPrinterSetup: TSpeedButton;
    btnImprimir: TSpeedButton;
    BtnCerrar: TSpeedButton;
    eDatos: TEdit;
    BtnAbrirFichero: TSpeedButton;
    BtnAbrirDatos: TSpeedButton;
    BtnImprimirSel: TSpeedButton;
    procedure BtnAbrirClick(Sender: TObject);
    procedure BtnDisenarClick(Sender: TObject);
    procedure BtnVerClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure BtnCerrarClick(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure btnConexionClick(Sender: TObject);
    procedure btnPrinterSetupClick(Sender: TObject);
    procedure btnImprimirClick(Sender: TObject);
    procedure eDividirKeyPress(Sender: TObject; var Key: Char);
    procedure BtnAbrirFicheroClick(Sender: TObject);
    procedure BtnImprimirSelClick(Sender: TObject);
    procedure BtnAbrirDatosClick(Sender: TObject);
  private
    { Private declarations }
    Procedure AbrirDatos;
    Procedure SetImpresora( Impr: String);
  public
    { Public declarations }
  end;

var
  FormMain: TFormMain;

implementation

uses
  ModDatos,
  frxClass;

{$R *.dfm}

procedure TFormMain.FormShow(Sender: TObject);
var
  IniFile: TIniFile;
begin
  cbImpresora.Clear;
  cbImpresora.Items.AddStrings( Printer.Printers);
  cbImpresora.Text := Printer.Printers[ Printer.PrinterIndex];

  IniFile := TIniFile.Create( ExtractFilePath( Application.ExeName) + 'Cod_Barras.ini');
  try
    eDatos.Text       := IniFile.ReadString( 'Cod_Barras', 'Datos', '');
    eReporte.Text     := IniFile.ReadString( 'Cod_Barras', 'Report', '');
    cbImpresora.Text  := IniFile.ReadString( 'Cod_Barras', 'Impresora', '');
  finally
    IniFile.Free;
  end;
  AbrirDatos;
end;

procedure TFormMain.BtnCerrarClick(Sender: TObject);
begin
  Close;
end;

procedure TFormMain.FormClose(Sender: TObject; var Action: TCloseAction);
var
  IniFile: TIniFile;
begin
  IniFile := TIniFile.Create( ExtractFilePath( Application.ExeName) + 'Cod_Barras.ini');
  IniFile.WriteString( 'Cod_Barras', 'Datos', eDatos.Text);
  IniFile.WriteString( 'Cod_Barras', 'Report', eReporte.Text);
  IniFile.WriteString( 'Cod_Barras', 'Impresora', cbImpresora.Text);
  IniFile.Free;
end;


procedure TFormMain.BtnAbrirClick(Sender: TObject);
begin
  OpenDialog.Filter     := 'FastReport 3 (*.fr3)|*.fr3';
  OpenDialog.InitialDir := ExtractFilePath( Application.ExeName);
  if OpenDialog.Execute then
    eReporte.Text := OpenDialog.FileName;
end;

procedure TFormMain.btnConexionClick(Sender: TObject);
begin
  OpenDialog.Filter     := 'Excel y Access (*.xls;*.mdb)|*.xls;*.mdb';
  OpenDialog.Filter     := 'Excel(*.xls)|*.xls';
  OpenDialog.InitialDir := ExtractFilePath( Application.ExeName);
  if OpenDialog.Execute then
  begin
    eDatos.Text := OpenDialog.FileName;
    AbrirDatos;
  end;

end;


procedure TFormMain.BtnDisenarClick(Sender: TObject);
begin
  if FileExists( eReporte.Text) then
    MD.frxReport.LoadFromFile( eReporte.Text);
  AbrirDatos;
  md.frxReport.PrintOptions.Printer := cbImpresora.Text;
  md.frxReport.DesignReport;
end;

procedure TFormMain.BtnVerClick(Sender: TObject);
begin
  if FileExists( eReporte.Text) then
    MD.frxReport.LoadFromFile( eReporte.Text);
  AbrirDatos;
  md.frxReport.PrintOptions.Printer := cbImpresora.Text;
  md.qDatos.DisableControls;
  try
    md.frxReport.PrepareReport();
    md.frxReport.ShowPreparedReport;
  finally
    md.qDatos.EnableControls;
  end;
end;

procedure TFormMain.AbrirDatos;
begin
  md.BBDD.Close;
  if ExtractFileExt( eDatos.Text) = '.xls' then
    md.BBDD.ConnectionString := 'Provider=Microsoft.Jet.OLEDB.4.0;Data Source="' + eDatos.Text + '";Extended Properties="Excel 8.0;HDR=Yes;IMEX=1";'
  else if ExtractFileExt( eDatos.Text) = '.mdb' then
    md.BBDD.ConnectionString := 'Provider=Microsoft.Jet.OLEDB.4.0;Data Source="' + eDatos.Text + '";User Id=admin;Password=;';

  if FileExists( eDatos.Text) then
    md.qDatos.Open;
end;

procedure TFormMain.btnPrinterSetupClick(Sender: TObject);
begin
  SetImpresora( cbImpresora.Text);
  PrinterSetupDialog.Execute;
end;

procedure TFormMain.eDividirKeyPress(Sender: TObject; var Key: Char);
begin
  if not ( Key in [ '0'..'9', #8, #13]) then Key := #0
end;

Procedure TFormMain.SetImpresora( Impr: String);
var
  idx: Integer;
begin
  //Obteniendo el indice de la impresora dentro de las impresorea del sistema
  //Si la actualmente seleccinada no es la deseada, se seleccina
  With Printer do
  begin
    Idx := Printers.Indexof( Impr );
    if PrinterIndex <> Idx then
      PrinterIndex := Idx
  end;
end;


procedure TFormMain.BtnAbrirFicheroClick(Sender: TObject);
begin
  md.BBDD.Close;
  ShellExecute(Handle,nil,PChar( eDatos.Text),'','',SW_SHOW);
end;

procedure TFormMain.btnImprimirClick(Sender: TObject);
begin
  if not FileExists( eReporte.Text) then exit;
  MD.frxReport.LoadFromFile( eReporte.Text);
  md.qDatos.DisableControls;
  try
    md.frxReport.PrepareReport();
    md.frxReport.Report.PrintOptions.Printer     := cbImpresora.Text;

    MD.frxReport.Print;
  finally
    MD.frxReport.Clear;
    md.qDatos.EnableControls;
  end;
end;



procedure TFormMain.BtnImprimirSelClick(Sender: TObject);
var
  Registro: String;
begin
  if not FileExists( eReporte.Text) then exit;
  Registro := IntToStr( md.qDatos.RecNo);
  md.qDatos.DisableControls;
  try
    MD.frxReport.LoadFromFile( eReporte.Text);
    md.frxReport.PrepareReport();
    md.frxReport.Report.PrintOptions.Printer     := cbImpresora.Text;

    //md.frxReport.Report.PrintOptions.PageNumbers := IntToStr( iDesde) + '-' + IntToStr( iHasta);
    md.frxReport.Report.PrintOptions.PageNumbers := Registro;
    md.frxReport.Report.PrintOptions.ShowDialog  := False;
    MD.frxReport.Print;
    MD.frxReport.Clear;
  finally
    md.qDatos.EnableControls;
  end;
end;

procedure TFormMain.BtnAbrirDatosClick(Sender: TObject);
begin
  AbrirDatos;
end;


end.
